package com.flight.trans.service;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.flight.trans.bean.MessageSocketBean;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.Url;
import com.flight.trans.util.JsonUtils;
import com.flight.trans.util.LogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;
import java.util.HashMap;
import java.util.Iterator;

/**
 * 消息服务
 */
public class MessageService extends Service {

    private Intent mMessageIntent;
    private Bundle mMessageBundle;
    private MessageSocketBean messageSocketBean;
    private JWebSocketClient jWebSocketClient;

    @Override
    public void onCreate() {
        super.onCreate();
        initSocketClient();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initSocketClient();
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 初始化长链接
     */
    private void initSocketClient() {
        if (jWebSocketClient != null)
            return;

        // 发送广播
        if (mMessageIntent == null)
            mMessageIntent = new Intent(Constant.MessageReceiver);

        jWebSocketClient = new JWebSocketClient(URI.create(Url.MessageSocketUrl + ConstantInfo.SOCKET_KEY)) {
            @Override
            public void onMessage(String message) {
                LogUtils.e( "接受消息" , message);

                if(ConstantInfo.MessageSocketSTR.equals(message))
                    return;

                ConstantInfo.MessageSocketSTR = message ;

                messageSocketBean = JsonUtils.parseJsonToBean(message, MessageSocketBean.class);

                if(messageSocketBean.getPrintType() == 1)
                    return;

                try {
                    JSONObject jsonObject = new JSONObject(message);
                    JSONArray sidArray = (JSONArray) jsonObject.get(Constant.Key_SID);
                    JSONObject sidJson = (JSONObject) sidArray.get(0);
                    Iterator<String> sidKeys = sidJson.keys();

                    while (sidKeys.hasNext()) {
                        messageSocketBean.setSidKey(sidKeys.next());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                // 存储
                if (!ConstantInfo.MessageSocketList.contains(messageSocketBean.getId())) {
                    ConstantInfo.MessageSocketList.add(messageSocketBean.getId());
                    senBroadcast();
                }
            }
        };
        jWebSocketClient.connect();
    }

    /**
     * 发送广播
     */
    private void senBroadcast (){
        try {
            mMessageBundle = new Bundle();
            mMessageBundle.putSerializable(Constant.INFO, messageSocketBean);
            mMessageIntent.putExtras(mMessageBundle);
            sendBroadcast(mMessageIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy () {
        if(jWebSocketClient != null)
            jWebSocketClient.close();
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind (Intent intent){
        return null;
    }
}

